package com.recharge.controller.jd;

import com.alibaba.fastjson.JSON;
import com.recharge.OrderService;
import com.recharge.domain.TradeOrder;
import com.recharge.mapper.ITradeOrderMapper;
import com.recharge.utils.OrderStatus;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by qi.cao on 2017/2/8.
 */
@Controller
@RequestMapping("/jd")
public class JDController {

    @Value("${jd.md5Key}")
    private String md5Key;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderService orderService;


    @Autowired
    private ITradeOrderMapper iTradeOrderMapper;

    /**
     * 是否能充值的查询
     * @param trafficPrepareSearch
     * @return
     */
    @RequestMapping("/trafficPrepareSearch")
    @ResponseBody
    public TrafficPrepareSearchResult trafficPrepareSearch(TrafficPrepareSearch trafficPrepareSearch){
        TrafficPrepareSearchResult trafficPrepareSearchResult = new TrafficPrepareSearchResult();
        trafficPrepareSearchResult.setIsSuccess(JdProtocol.SUCCESS);
        return trafficPrepareSearchResult;
    }


    /**
     * 充值请求
     * @param trafficBeginFill
     * @return
     */
    @RequestMapping("/trafficBeginFill")
    @ResponseBody
    public TrafficBeginFillResult trafficBeginFill(TrafficBeginFill trafficBeginFill){
        TrafficBeginFillResult trafficBeginFillResult = new TrafficBeginFillResult();

        String sourceString = trafficBeginFill.getSignString()+md5Key;

        logger.info("收到JD 充值请求，加密原串是：{}",sourceString);

        if (!StringUtils.equals(trafficBeginFill.getSign(), DigestUtils.md5Hex(sourceString))){

            logger.error("JD 充值请求，md5 校验不通过,加密后的串:{}",DigestUtils.md5Hex(sourceString));
            trafficBeginFillResult.setIsSuccess(JdProtocol.FAIL);
            trafficBeginFillResult.setErrorCode("JDI_00002");
            return trafficBeginFillResult;
        }

        String fillType = trafficBeginFill.getFillType();
        String packageType= "";
        if (StringUtils.equals("0",fillType)){
            packageType = "000";
        }else if (StringUtils.equals("1",fillType)){
            packageType = "003";
        }

        String areaUsed = trafficBeginFill.getAreaUsed();
        Short busiType= 0;
        if (StringUtils.equals("0",areaUsed)){
            busiType = 4;
        }else{
            busiType = 5;
        }

        orderService.makeOrder("JDtest",
                Short.parseShort(JdMapping.ispMapper.get(trafficBeginFill.getMutCode())),
                JdMapping.provinceMapper.get(trafficBeginFill.getAreaCode()),
                "",
                trafficBeginFill.getFillMobile(),
                Long.parseLong(trafficBeginFill.getFillAmount()),
                trafficBeginFill.getFillOrderNo(),
                trafficBeginFill.getNotifyUrl(),
                busiType,
                0L,
                trafficBeginFill.getCostPrice(),
                packageType,
                "LL"
                );

        return trafficBeginFillResult;
    }

    /**
     * 订单查询接口
     * @return
     */
    @RequestMapping("/trafficFindFill")
    @ResponseBody
    public TrafficFindFillResult trafficFindFill(TrafficFindFill trafficFindFill){
        TrafficFindFillResult trafficFindFillResult= new TrafficFindFillResult();
        trafficFindFillResult.setIsSuccess(JdProtocol.SUCCESS);

        String sourceString = trafficFindFill.getSignString()+md5Key;

        logger.info("收到JD 充值请求，加密原串是：{}",sourceString);

        if (!StringUtils.equals(trafficFindFill.getSign(), DigestUtils.md5Hex(sourceString))){

            logger.error("JD 充值请求，md5 校验不通过,加密后的串:{}",DigestUtils.md5Hex(sourceString));
            trafficFindFillResult.setIsSuccess(JdProtocol.FAIL);
            trafficFindFillResult.setErrorCode("JDI_00002");
            return trafficFindFillResult;
        }


        TradeOrder tradeOrder = iTradeOrderMapper.selectByMerOrderId(trafficFindFill.getFillOrderNo());

        logger.info("query order result:{}", JSON.toJSONString(tradeOrder));
        if (tradeOrder!=null){
            if (StringUtils.equals(tradeOrder.getOrderStatus()+"", ""+OrderStatus.SUCCESS)){
                trafficFindFillResult.setFillStatus("1");
            }else if (StringUtils.equals(tradeOrder.getOrderStatus()+"", ""+OrderStatus.FAIL)){
                trafficFindFillResult.setFillStatus("2");
            }else{
                trafficFindFillResult.setFillStatus("3");
            }
        }else{
            trafficFindFillResult.setIsSuccess(JdProtocol.FAIL);
            trafficFindFillResult.setErrorCode("JDI_00007");
        }


        return trafficFindFillResult;
    }
}
